package kz.dasm.baing.rest;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import kz.dasm.baing.rest.entity.GenericResponse;

/**
 *
 * @author Kreedmart
 */
@Provider
public class SQLExceptionMapper implements ExceptionMapper<SQLException> {

  static final private Logger logger = Logger.getLogger(
          SQLException.class.getName());
  
  public Response toResponse(SQLException e) {
    logger.log(Level.WARNING, "SQL exception thrown", e);
    
    GenericResponse resp = new GenericResponse();
    resp.setSuccess(false);
    resp.setErrorMessage(e.getMessage());
    
    return Response.ok().type(MediaType.APPLICATION_JSON).entity(resp).build();
  }
  
}
